package com.sanhe.rfmuscle.activity;

import static com.menu.app.util.FunctionCode.START1;
import static com.menu.app.util.FunctionCode.START2;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.MenuProvider;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

import com.menu.app.MyApp;
import com.menu.app.activity.BaseBoardActivity;
import com.menu.app.event.StartEvent;
import com.menu.app.model.Models;
import com.menu.app.util.port.SerialManage;
import com.sanhe.rfmuscle.util.Const;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.drakeet.support.toast.ToastUtil;

public class BoardActivity extends BaseBoardActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!Models.Companion.get().getStartEvent().getValue()) {
            showLoading(getString(com.menu.app.R.string.init));
        }
        binding.camera.setVisibility(View.INVISIBLE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(StartEvent event) {
        checkStart();
    }

    @Override
    protected boolean checkHandle(Runnable runnable) {
        boolean bo = Models.Companion.get().getStartEvent().getValue();
        if (!bo) {
            hideLoading();
            ToastUtil.showShortToast(MyApp.getApp(), MyApp.getApp().getString(com.menu.app.R.string.disconnect));
            runnable.run();
        }
        return bo;
    }

    private void checkStart() {
        boolean bo = Models.Companion.get().getStartEvent().getValue();
        if (!bo) {
            return;
        }
        String tmp = getString(com.menu.app.R.string.init);
        if (tmp.equals(getLoadingMsg())) {
            hideLoading();
        }
    }

    @Override
    protected void checkState(boolean jump) {
        if (!jump) {
            return;
        }
        if (!Const.jump){
            if (!Models.Companion.get().getStartEvent().getValue()) {
                ToastUtil.showShortToast(BoardActivity.this, com.menu.app.R.string.disconnect);
                return;
            }
        }

        toWork();
    }

    @Override
    protected String getReportUrl() {
        return Const.HISTORY;
    }

    @Override
    protected void sendTouchEvent(byte data_H, byte data_L) {
        byte[] tmp = {START1, START2, 6, -125, 16, 1, 1, data_H, data_L};
        SerialManage.getInstance().send(tmp);
    }

    @Override
    public void addMenuProvider(@NonNull MenuProvider provider, @NonNull LifecycleOwner owner, @NonNull Lifecycle.State state) {

    }
}
